package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

public class Solution_0563 {
    /**
     * @param root
     * @return
     */
    public int findTilt(TreeNode root) {
        this.dfs01(root);
        return res;

    }

    public int res = 0;

    public void dfs01(TreeNode root) {
        if (root == null) {
            return;
        }

        int left = dfs(root.left);
        int right = dfs(root.right);

        res += Math.abs(left - right);
        dfs01(root.left);
        dfs01(root.right);

    }

    public int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        return root.val + left + right;
    }
}
